package com.atguigu.edu.realtime220815.app.dwd.db;

import com.atguigu.edu.realtime220815.util.KafkaUtils;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import java.time.Duration;

public class DwdTradeOrderDetail {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        tableEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(5));

        tableEnv.executeSql(KafkaUtils.getTopicDbDDL("dwd_trade_order_detail"));
        // TODO 4. 读取订单明细表数据
        Table orderDetail = tableEnv.sqlQuery("select \n" +
                "data['id'] id,\n" +
                "data['order_id'] order_id,\n" +
                "data['course_id'] course_id,\n" +
                "data['course_name'] course_name,\n" +
                "data['create_time'] create_time,\n" +
                "data['user_id'] user_id,\n" +
                "data['coupon_reduce'] coupon_reduce,\n" +
                "data['final_amount'] final_amount,\n" +
                "data['origin_amount'] origin_amount,\n" +
                "ts\n" +
                "from `topic_db` where `table` = 'order_detail' " +
                "and `type` = 'insert'\n");
        tableEnv.createTemporaryView("order_detail", orderDetail);
        //tableEnv.executeSql("select * from topic_db");

        // TODO 5. 读取订单表数据
        Table orderInfo = tableEnv.sqlQuery("select \n" +
                "data['id'] id,\n" +
                "data['user_id'] user_id,\n" +
                "data['coupon_reduce'] coupon_reduce,\n" +
                "data['final_amount'] final_amount,\n" +
                "data['origin_amount'] origin_amount,\n" +
                "data['province_id'] province_id,\n" +
                "data['order_status'] order_status\n" +
                "from `topic_db`\n" +
                "where `table` = 'order_info'\n" +
                "and `type` = 'insert'");
        tableEnv.createTemporaryView("order_info", orderInfo);


        // TODO 6. 建立 MySQL-LookUp 字典表
        //tableEnv.executeSql(MySqlUtil.getBaseDicLookUpDDL());

        // TODO 7. 关联三张表获得订单明细表
        Table resultTable = tableEnv.sqlQuery("select \n" +
                "od.id,\n" +
                "od.order_id,\n" +
                "oi.user_id,\n" +
                "od.course_id,\n" +
                "od.course_name,\n" +
                "date_format(od.create_time, 'yyyy-MM-dd') date_id,\n" +
                "oi.order_status,\n" +
                "oi.province_id,\n" +
                "od.origin_amount,\n" +
                "od.final_amount,\n" +
                "od.coupon_reduce,\n" +
                "od.ts \n" +
                "from order_detail od \n" +
                "join order_info oi\n" +
                "on od.order_id = oi.id"

);
        tableEnv.createTemporaryView("result_table", resultTable);

        // TODO 8. 建立 Upsert-Kafka dwd_trade_order_detail 表
        tableEnv.executeSql("create table dwd_trade_order_detail(\n" +
                "id string,\n" +
                "order_id string,\n" +
                "user_id string,\n" +
                "course_id string,\n" +
                "course_name string,\n" +
                "date_id string,\n" +
                "order_status string,\n" +
                "province_id string,\n" +
                "origin_amount string,\n" +
                "final_amount string,\n" +
                "coupon_reduce string,\n" +
                "ts string,\n" +
                "primary key(id) not enforced\n" +
                ")" + KafkaUtils.getUpsertKafkaDDL("dwd_trade_order_detail"));

        // TODO 9. 将关联结果写入 Upsert-Kafka 表
        tableEnv.executeSql("select * from result_table");
        //tableEnv.executeSql("insert into dwd_trade_order_detail select * from result_table");

    }
}
